/*
 *  dcc/dccmain.c
 *
 *  Copyright (C) 2008  Jeongho Nah
 *
 *  Support of [company] added by [name], August 2008
 *
 *  Memory region support
 *	[name] [email], July-August 1999
 *
 */

/*
 * This file handles the main parts of compilation
 * First version!
 * Jeongho Nah / njh1983@korea.com / August 2008
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dccstdlib.h"
#include "dccstdtype.h"

/* This global vars are also used in internal functions : parser.c */
FILE *sourceFile, *astFile, *ucodeFile; /* DO NOT CHANGE! */

int main(int argc, char *argv[])
{
	char fileName[50];
	Node *root;

	/***** Printing start messages *****/
	printf(" === Staring DCC... === \n\n");
	printf("DCC is a acronym of Dokdo C-language Compiler.\n");
	printf("Do you know  what Dokdo is? Shortly,\n");
	printf("Dokdo is a group of disputed islets \n");
	printf("in the East Sea administered by South Korea.\n");
	printf("Visit: http://dccproject.tistory.com for more infomations.\n");
	printf("Thanks.\n\n\n");

	/***** Input from command line prompt *****/
	if (argc !=2) {
		printf("NOT ENOUGH ARGVs.\n");
		printf("Usage$ dcc sampledcc.c\n\n\n");
		exit(1);
	}

	/***** File controls *****/
	strcpy(fileName, argv[1]);
	printf(" >> source: %s\n", fileName);

	if ((sourceFile = fopen(fileName, "r")) == NULL) {
		printf("fopen error %s\n", fileName);
		exit(1);
	}
	astFile = fopen(strcat(strtok(fileName, "."), ".ast"), "w");
	ucodeFile = fopen(strcat(strtok(fileName, "."), ".uco"), "w");

/*****************************************************************/
/***** main part *****/

	/* === start of Parser === */
	printf(" === Staring Parser... === \n");
	root = parser();
	printTree(root, 0);

	/* === start of ICG === */
	printf(" === Starting Code Generator... ===\n");
	codeGen(root);
/*****************************************************************/


	/***** Print ending messages *****/
	printf(" === end of DCC ===\n");

	fclose(astFile);
	fclose(sourceFile);
	return 0;
}